 /**
  * Created by ex-luandebiao001 on 2015/2/11.
 */
;
(function(){
    'use strict';
   
    var app = angular.module('fwa.mainApp');
    
    app.controller('performanceIndicatorsCtrl',['$scope','$http','Modal','BaseURL',function($scope,$http,Modal,baseURL){
       /*初始化数据*/ 
       $scope.init = function(){
           $http({
               url:baseURL+'assessment/findIndicators.do',
               method:'GET',
               cache:false
           }).success(function(resp){
               if(resp.state!=='0'){
                   Modal.alert(resp.msg);
               }
               $scope.resp = resp.result;
               $scope.dataStructure = $scope.resp.dataStructure;
               $scope.data = $scope.resp.all;
               $scope.level0 = $scope.resp.level0;
               $scope.level1 = $scope.resp.level1; 
               $scope.level2 = $scope.resp.level2;
             //调用
               $scope.analyticalData($scope.dataStructure);
           }); 
       }
       
       
        $scope.init(); 
       
        $scope.nullFlag = false;

        $scope.btnFlag = false;

        $scope.ulFlag = false;

        $scope.define = function(){
            $scope.btnFlag = true;
            $scope.ulFlag = false;
        }
        
        $scope.cancel = function(){
            $scope.btnFlag = false;
            $scope.ulFlag = false;
            $scope.selectedShow =false;
            $scope.init(); 
        }
        
        $scope.result = [];
        
        
        
        
        
        
        /**
         * 分析数据 在页面上展示
         */
        $scope.analyticalData = function(dataStructure) {
            var length = dataStructure.list.length;
            if (length > 1) {
                dataStructure.twoWidth = parseFloat(100/length);
                dataStructure.oneWidth = parseFloat(100 - dataStructure.twoWidth);
                angular.forEach(dataStructure.list,function(twoData,i) {
                    var threeLength = twoData.list.length;
                    if (threeLength > 1) {
                        twoData.fourWidth = parseFloat(100/threeLength);
                        twoData.threeWidth = parseFloat(100 - twoData.fourWidth);
                    }
                    else {
                        twoData.fourWidth = 100;
                    }
                });
            }
            else {
                dataStructure.twoWidth =100;
            }
        }
        
        
        //存储已经请求过的指标值集合
        var kpiDataMap = {};
        
        //是否显示指标集合
        $scope.selectedShow = false;
        
        //三角形距离左边的位置
        $scope.beforeLeft = 8;
        
        $scope.afterLeft = 9;
        
        //点击事件
        $scope.selectData = function(dataStructure) {
            
            if ($scope.selectedShow == false) {
                $scope.selectedShow = true;
            }
            $scope.selectedData = dataStructure;
            angular.forEach($scope.dataStructure.list,function(twoData,i) {
                angular.forEach(twoData.list,function(threeData,j) {
                    if (threeData == dataStructure) {
                        //三角形跟随选中的三级树形移动
//                      var offsetWidth = document.body.offsetWidth - 150;
//                      var left = parseFloat(offsetWidth * $scope.dataStructure.twoWidth * i / 100) +
//                      parseFloat(offsetWidth * $scope.dataStructure.twoWidth * twoData.fourWidth * (j + 0.5) / 10000);
//                      if (i > 0 && j == 0) {
//                          $scope.beforeLeft = parseFloat(left - 7).toFixed(2);
//                          $scope.afterLeft = parseFloat(left - 6).toFixed(2);
//                      }
//                      else if (i > 0 && j == 1) {
//                          $scope.beforeLeft = parseFloat(left - 10).toFixed(2);
//                          $scope.afterLeft = parseFloat(left - 9).toFixed(2);
//                      }
//                      else if (i > 0 || (i >= 0 && j > 1)) {
//                          $scope.beforeLeft = parseFloat(left - 15).toFixed(2);
//                          $scope.afterLeft = parseFloat(left - 14).toFixed(2);
//                      }
//                      else {
//                          $scope.beforeLeft = parseFloat(left).toFixed(2);
//                          $scope.afterLeft = parseFloat(left + 1).toFixed(2);
//                      }
                        
                        threeData.selected = true;
                    }
                    else {
                        threeData.selected = false;
                    }
                });
            });
           /* //获取展示指标数据
            if (kpiDataMap[dataStructure.code]) {
                $scope.kpiSelectedDatas = kpiDataMap[dataStructure.code];
            }
            else {
                //请求后台数据 请后续修改，暂时只做测试使用
                $scope.kpiSelectedDatas = test(dataStructure.code);
                kpiDataMap[dataStructure.code] = $scope.kpiSelectedDatas;
            }*/
        }
        
        
        
        
        
        
        
        
        
        
        
        
        /*验证数据*/
        $scope.$watch('data',function(newValue,oldValue){
            var keepGoing = true;
            var regu =/^\d{1,2}(\.\d{0,1})?$/;
            var re = new RegExp(regu);
            angular.forEach(newValue,function(item,key){
                if(keepGoing){
                    if(!re.test(item.value)){
                        if(item.value!==100){
                            $scope.nullFlag=true;
                            keepGoing=false;
                        }
                    }else{
                        $scope.nullFlag=false;
                    }
                }
            });
        },true);
        
        /*统计保存数据总分值,验证用*/
        $scope.countTotal=function(){
            var total =0;
            
            var totalArr = $scope.transform($scope.level0,$scope.data);
            
            angular.forEach(totalArr,function(item,key){
                var _value = item.value;
                total+=_value;
            });
            
            return total;
        }
        
        /*顶级指标展示并绑定数据*/
        $scope.countTop=function(type){
            var total=0;
            if(type){
                angular.forEach($scope.resp[type],function(item,key){
                    angular.forEach($scope.resp[item.code],function(_item,_key){
                        $scope.countTopArr = [];
                        $scope.countTopArr = $scope.transform($scope.resp[_item.code],$scope.data);
                        angular.forEach($scope.countTopArr,function(item3,key3){
                            var _value = item3.value;
                            if(isNaN(_value)){
                                _value = 0; 
                            }
                            total+=_value;
                        });
                    });
                });
               
                if(isNaN(total)){
                    total=0;
                    return total;
                }
                
                angular.forEach($scope.data,function(_item,_key){
                    if(type===_item.code){
                        _item.value = parseFloat(total.toFixed(1));
                    }
                });
                
            }
            
            return parseFloat(total.toFixed(1));
        }
        /*数据转换为需保存数据*/
        $scope.transform = function(original,target){
            var arr=[];
            
            angular.forEach(original,function(item,key){
                angular.forEach(target,function(_item,_key){
                    if(item.code===_item.code){
                        arr.push(_item);
                    }
                });
            });
           
            return arr;
        }
        
        /*二级指标展示并绑定数据*/
        $scope.countSecondly=function(type){
            
            var total=0;
            $scope.countSecondlyArr = [];
           
            $scope.countSecondlyArr = $scope.transform($scope.resp[type],$scope.data);
            angular.forEach($scope.countSecondlyArr,function(item,key){
                var _value = item.value;
                if(isNaN(_value)){
                    _value = 0; 
                }
                total+=_value;
            });
            if(isNaN(total)){
                total=0;
                return total;
            }
            angular.forEach($scope.data,function(_item,_key){
                if(type===_item.code){
                    _item.value = parseFloat(total.toFixed(1));
                }
            });
            return parseFloat(total.toFixed(1));
        }
        
        /*一级指标展示并绑定数据*/
        $scope.countPrimary=function(type){
            var total=0;
            
            angular.forEach($scope.resp[type],function(item,key){
                $scope.countPrimaryArr = [];
                $scope.countPrimaryArr = $scope.transform($scope.resp[item.code],$scope.data);
                angular.forEach($scope.countPrimaryArr,function(item,key){
                    var _value = item.value;
                    if(isNaN(_value)){
                        _value = 0; 
                    }
                    total+=_value;
                });
            });
            
            if(isNaN(total)){
                total=0;
                return total;
            }
            
            angular.forEach($scope.data,function(_item,_key){
                if(type===_item.code){
                    _item.value = parseFloat(total.toFixed(1));
                }
            });
            
            return parseFloat(total.toFixed(1));
        }

        
        /*展示三级指标*/
        $scope.show = function(target){
            if($scope.btnFlag){
                $scope.variate = [];
                
                if ($scope.selectedShow == false) {
                    $scope.selectedShow = true;
                }
                angular.forEach($scope.dataStructure.list,function(twoData,i) {
                    angular.forEach(twoData.list,function(threeData,j) {
                        if (threeData == target) {
                            threeData.selected = true;
                        }
                        else {
                            threeData.selected = false;
                        }
                    });
                });
                
                $scope.ulFlag = true;
                var type = target.code;
                
                $scope.variate = $scope.transform($scope.resp[type],$scope.data);
               
                angular.forEach($scope.data,function(item,key){
                    if(item.code===type){
                        $scope.listTitle= item.name;
                    }
                });
                $scope.listScore= function(){
                    return $scope.countSecondly(type);
                };
            }
        }

      
        /*保存指标*/
        $scope.save = function(){
            if($scope.countTotal()===100&&!$scope.nullFlag){
                var data = angular.toJson($scope.data);
                $http.post(baseURL + 'assessment/saveIndicators.do', data).success(function(response){
                    Modal.alert(response.msg);
                })
            }else{
                Modal.alert('数据请符合规范,且总占比应为100');
            }
        }
        
    }]);
    
})();